// pages/introduce/introduce.js
import ajax from "../../utils/ajax";
import util from "../../utils/util";
import { startGlobalTimer } from '../../utils/timer'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNextDesc:true,
    countdownTime: 60, // 倒计时时间
  },
  onReady(){
    // 页面进入倒计时
    this.startCountdown();
  },
  onTap(){
    setTimeout( () => {
      this.setData({
        isNextDesc:false
      })
    },500)
    // 清除倒计时
    if (this.data.countdownInterval) {
      clearInterval(this.data.countdownInterval);
      this.setData({ countdownInterval: null });
    }
    // 页面进入倒计时
    this.startCountdown();
  },
  // 开始答题
  onStar(){
  //  this.getTopicData();
    startGlobalTimer();//开始计时
    wx.setStorageSync('startTime', Date.now());//记录开始答题的时间戳
    util.navigateByDesc(0);
    this.startTest()
  },
  startTest(){
    const userInfo = wx.getStorageSync('userInfo');
    let idcard = userInfo.idcard;
    const answerWork = userInfo.unitId
    const name = userInfo.name
    ajax.postMethod('small/app/wx/start',{
      idCard: idcard,
      name,
      answerWork:answerWork
    })
  },
  // 获取认知题目
  async getTopicData() {
    const userInfo = wx.getStorageSync('userInfo');
    let idcard = userInfo.idcard;
    let getType = await ajax.getMethod('small/app/paper/list/'+ idcard+'/'+userInfo.unitId);
    if(getType.code !== 200){
      wx.showToast({
        title: getType.msg,
        icon:'none'
      })
    }
    let type = getType.data.find(item => item.ptype == 1);
 
    let anotherType = getType.data.find(item => item.ptype === 2);
    const mentalInfo = getType.data.find(item => item.ptype === 3);
    //胜任
    wx.setStorageSync('anotherType',anotherType?.paperId);
    //心理
    wx.setStorageSync('mentalId',mentalInfo?.paperId);

    //认知
    wx.setStorageSync('bussId',type?.paperId);
    //当前试卷id
    wx.setStorageSync('paperId',type?.paperId);
    wx.setStorageSync('testType',2);
    const answerWork = userInfo.unitId
    const res = await ajax.getMethod('small/app/paper/info/' + type.paperId + '/' +idcard+'/'+answerWork)
    const questionList = res.data.paperQuestionTypes;
    const questionNumber = 0;
    // 将 questionList 存入缓存
    wx.setStorageSync('questionInfo', {
      questionList,
      questionNumber
    });
   
    // 跳转对应页面
    if(type?.completed!=1){
      util.navigateByDesc(questionNumber);
    }
    // 答题数据缓存
    wx.setStorageSync('answerBaseArray', {
      name:userInfo.name,
      cardId:userInfo.idcard,
      instructorName:'',
      instructorId:0,
      paperId:type?.paperId,
      deptId:'',
      areaId:userInfo.areaId,
      answerWork:userInfo.unitId
    });

    if(type?.completed==1){
      //全部完成考试
      if(anotherType?.completed==1&&mentalInfo?.completed==1){
        wx.showToast({
          title: '你已完成考试',
          icon:'none'
        })
        setTimeout(() => {
          wx.reLaunch({
            url: '../index/index?cleaned=1',
          })
        }, 1000);
       
      }else if(anotherType?.completed==1){
        wx.reLaunch({
          url: '../topic-another/topic-another?id='+ mentalInfo?.paperId
        })
      }else{
        wx.reLaunch({
          url: '../topic-another/topic-another?id='+ anotherType?.paperId
        })
      }
      return
    }
  },
  // 倒计时
  startCountdown() {
    // 清除已有倒计时
    if (this.data.countdownInterval) {
      clearInterval(this.data.countdownInterval);
    }
    this.setData({
      countdownTime: 60
    });
    const interval = setInterval(() => {
      let newTime = this.data.countdownTime - 1;
      if (newTime > 0) {
        this.setData({
          countdownTime: newTime
        });
      } else {
        clearInterval(interval);
        // 倒计时结束，进入下一步
        wx.showToast({
          title: '进入下一步',
          icon: 'none',
          duration: 1000
        });
        setTimeout(() => {
          if(this.data.isNextDesc){
            this.setData({
              isNextDesc:false
            })
            this.startCountdown()
          }else{
            this.onStar();
          }
        }, 1000);
      }
    }, 1000);
    this.setData({
      countdownInterval: interval
    });
  },
  onUnload() {
    // 清除定时器
    if (this.data.countdownInterval) {
      clearInterval(this.data.countdownInterval);
      this.setData({ countdownInterval: null });
    }
  }
})